import docx
from docx import Document
import xlwt
import os
import shutil
# from test2 import getFiles

def readFile(filpath,filename):

	# 新建excel表单
	workbook = xlwt.Workbook(encoding='utf-8')
	sheet = workbook.add_sheet("sheet1")

	# 取到文件夹里的文件路径和文件名
	docFile = filpath
	renameFile = filename

	document = Document(docFile)    #读入文件
	tables = document.tables 		#获取文件中的表格集

	table = tables[0]
	row_count = len(table.rows)
	col_count = len(table.columns)


	my_list = []
	# 循环遍历world 里的表格每个单元格的内容放入列表
	for table in tables:
		for row in table.rows:
			for cell in row.cells:
				my_list.append(cell.text)

	# print(my_list)

	# 筛选删除重复数据
	list4=[]
	for i in my_list:
		if not i in list4:
			list4.append(i)

	# print(list4)


	idArr = []
	namesArry = []
	sexNum = []
	studyID =[]


	num_index = 1
	index = 1;
	sex_index = 1
	sty_index = 1

	sheet.write(0,0,'序号')
	sheet.write(0,1,'姓名')
	sheet.write(0,2,'性别')
	sheet.write(0,3,'学生编号')


	recoder = 0 #序号
	for item in list4:
		str = '姓名：'
		if(str in item):
			if(item[3:7]!=''):
				recoder+=1
				namesArry.append(item[3:7])
				idArr.append(recoder)

		str2 = '学籍号：'
		if (str2 in item):
			if(item.split('：')[1]!=''):
				studyID.append(item.split('：')[1])

		str3 = '编号'
		if(str3 in item):
			sex_str = item.split('：')[1]
			if(sex_str!=''):
				if('女' in sex_str):
					sexNum.append('女')
				elif('男' in sex_str):
					sexNum.append('男')
				else:sexNum.append('无性别')

	# 序号
	for num in idArr:
		sheet.write(num_index,0,num)
		num_index+=1

	# 姓名
	for name in namesArry:

		sheet.write(index, 1, name)
		index+=1

	# 性别
	if(len(sexNum)!=0):
		for sex in sexNum:
			sheet.write(sex_index,2,sex)
			sex_index+=1

	# 学籍号
	for study in studyID:

		sheet.write(sty_index,3,study)
		sty_index+=1

	file_name = renameFile+".xls"
	workbook.save(file_name)

	# temppath = os.getcwd()

	# 移动文件到指定路径
	# file_path = os.path.join(temppath,file_name)

	# shutil.move(file_path, targetfile)

	# print(file_path)
	# print(namesArry,idArr,studyID)